#include "wtkpc.h"

#include "wtk/ParentWindow.h"

namespace wtk
{
	bool ParentWindow::GetChildWindowBoundingBox(Box &boundingBox)
	{
		size_t n = childWindows.size();
		if (n == 0) {
			return false;
		} else {
			Window *childWindow = childWindows[0];
			Point topLeftCornerOfBoundingBox(childWindow->GetTopLeftCorner());
			Point bottomRightCornerOfBoundingBox(childWindow->GetBottomRightCorner());
			for (size_t i = 1; i < n; i++) {
				childWindow = childWindows[i];
				Point topLeftCornerOfWindow(childWindow->GetTopLeftCorner());
				if (topLeftCornerOfWindow.x < topLeftCornerOfBoundingBox.x) {
					topLeftCornerOfBoundingBox.x = topLeftCornerOfWindow.x;
				}
				if (topLeftCornerOfWindow.y < topLeftCornerOfBoundingBox.y) {
					topLeftCornerOfBoundingBox.y = topLeftCornerOfWindow.y;
				}
				Point bottomRightCornerOfWindow(childWindow->GetBottomRightCorner());
				if (bottomRightCornerOfWindow.x > bottomRightCornerOfBoundingBox.x) {
					bottomRightCornerOfBoundingBox.x = bottomRightCornerOfWindow.x;
				}
				if (bottomRightCornerOfWindow.y > bottomRightCornerOfBoundingBox.y) {
					bottomRightCornerOfBoundingBox.y = bottomRightCornerOfWindow.y;
				}
			}
			boundingBox.SetCornerPoints(topLeftCornerOfBoundingBox, bottomRightCornerOfBoundingBox);
			return true;
		}
	}

	void ParentWindow::AdjustChildWindowPositions(int dX, int dY)
	{
		for (size_t i = 0, n = childWindows.size(); i < n; i++) {
			Window *childWindow = childWindows[i];
			childWindow->position.x += dX;
			childWindow->position.y += dY;
		}
	}

	void ParentWindow::CommitChildWindowPositions()
	{
		UINT flags = SWP_NOACTIVATE | SWP_NOCOPYBITS | SWP_NOOWNERZORDER | SWP_NOREDRAW | SWP_NOZORDER;

		HDWP dwp = win32::BeginDeferWindowPos(childWindows.size());
		for (size_t i = 0, n = childWindows.size(); i < n; i++) {
			dwp = childWindows[i]->DeferWindowPos(dwp, NULL, flags);
		}
		win32::EndDeferWindowPos(dwp);
	}

	void ParentWindow::Layout()
	{
		for (size_t i = 0, n = childWindows.size(); i < n; i++) {
			childWindows[i]->Layout();
		}

	}
}
